<template>
  <a-modal
    :title="PageTitle"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="委外编号" prop="Code">
              <a-input v-model="entity.Code" autocomplete="off" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="委外名称" prop="MaterialName">
              <a-input v-model="entity.MaterialName" autocomplete="off" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="计划完成工日期" prop="Model">
              <a-input v-model="entity.Model" autocomplete="off" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>

          <a-col :span="8">
            <a-form-model-item label="供应商" prop="Spec">
              <a-input v-model="entity.Spec" autocomplete="off" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="申请人" prop="Unit">
              <a-input v-model="entity.Unit" autocomplete="off" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="备注" prop="Unit">
              <a-input v-model="entity.Unit" autocomplete="off" />
            </a-form-model-item>
          </a-col>
        </a-row>

      </a-form-model>
      <a-card title="委外商品列表（温馨提示：保存时，系统会根据BOM表自动计算出【所需物料】）">
        <a slot="extra" href="#">添加委外商品</a>
        <a-table
          ref="table"
          :columns="columns"
          :rowKey="row => row.Id"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :bordered="true"
          size="small"
        >
          <span slot="action" slot-scope="text, record">
            <template>
              <a @click="handleEdit(record.Id)">编辑</a>
              <a-divider type="vertical" />
              <a @click="handleDelete([record.Id])">删除</a>
            </template>
          </span>
        </a-table>
      </a-card>
    </a-spin>
  </a-modal>
</template>

<script>
const columns = [
  { title: '产品编号', dataIndex: 'Code', width: '10%' },
  { title: '产品名称', dataIndex: 'MaterialName', width: '10%' },
  { title: '型号', dataIndex: 'Model', width: '20%' },
  { title: '规格', dataIndex: 'Spec', width: '30%' },
  { title: '类型', dataIndex: 'MaterialType', width: '10%' },
  { title: '单位', dataIndex: 'Unit', width: '10%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
]

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data () {
    return {
      PageTitle: '新增委外加工',
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      confirmLoading: false,
      entity: {},
      rules: {
        Code: [{ required: true, message: '必填' }],
        MaterialName: [{ required: true, message: '必填' }]
      },
      data: [],
      pagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id) {
      this.init()

      if (id) {
        this.$http.post('/Aps/Aps_Material/GetTheData', { id: id }).then(resJson => {
          this.entity = resJson.Data
          // if (this.entity['Birthday']) {
          //  this.entity['Birthday'] = moment(this.entity['Birthday'])
          // }
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.confirmLoading = true
        this.$http.post('/Aps/Aps_Material/SaveData', this.entity).then(resJson => {
          this.confirmLoading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.afterSubmit()
            this.visible = false
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Aps/Aps_Material/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.submitDelete(ids, resolve, reject)
          }).catch(() => console.log('Oops errors!'))
        }
      })
    }
  }
}
</script>
